﻿using System;
using System.Collections.Generic;
using System.Text;
using Mang24k99.Model.PUBC;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord.Queries;
namespace Mang24k99.Business.PUBC
{
    public partial class BKtPubcProduct
    {
        public static void Create(MKtPubcProduct model)
        {
            model.IsValidate = 1;
            model.InputTime = DateTime.Now;
            model.ModifyTime = DateTime.Now;
            model.DeleteTime = DateTime.Now;
            model.Create();
        }
        public static void Update(MKtPubcProduct model)
        {
            model.ModifyTime = DateTime.Now;
            model.Update();
        }
        public static void Delete(MKtPubcProduct model)
        {
            //删除默认字段在ondelete中执行
            model.Delete();
        }

        public static IList<MKtPubcProduct> FindAll()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcProduct.SelectAll(criteria);
        }

        public static MKtPubcProduct Find(object ID)
        {
            return MKtPubcProduct.SelectByPrimaryKey(ID);
        }
        public static MKtPubcProduct FindOne()
        {
            ICriterion[] criteria = new ICriterion[]
            {
            };
            return MKtPubcProduct.SelectOne(criteria);
        }
        public static MKtPubcProduct FindFirst()
        {
            ICriterion[] criteria = new ICriterion[]
            {
            };
            return MKtPubcProduct.SelectFirst(criteria);
        }

        public static IList<MKtPubcProduct> FindALLByProperty(string property, object value)
        {
            return MKtPubcProduct.SelectALLByProperty(property, value);
        }

        public static IList<MKtPubcProduct> FindALLSliced(int firstResult, int maxResult)
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcProduct.SelectALLSliced(firstResult, maxResult, criteria);
        }
        public static IList<MKtPubcProduct> FindALLQuery(int firstResult, int maxResult)
        {
            string strHql = @"Select MKtPubcProduct"
                          + " from"
                          + " MKtPubcProduct MKtPubcProduct"
                          + " where"
                          + " MKtPubcProduct.IsValidate = 1"
                          ;
            SimpleQuery query = new SimpleQuery(typeof(MKtPubcProduct), strHql);
            query.SetQueryRange(firstResult, maxResult);
            return MKtPubcProduct.SelectAllQuery(query);
        }
        public static int Count()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcProduct.Counts(criteria);
        }
        public static bool Exists(object id)
        {
            return MKtPubcProduct.Exist(id);
        }
        /// <summary>
        /// 验证名称是否重复(重复则返回false)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool NameValidate(string name)
        {
            string where = " where product.BoardName='"+name+"'";
            MKtPubcProduct[] productlist = (MKtPubcProduct[])MKtPubcProduct.SelectByWhere(where);
            if (productlist.Length>0)
            {
                return false;
            }
            else  
            {
                return true;
            }
        }
       /// <summary>
       /// 验证调查表简称是否重复
       /// </summary>
       /// <param name="alias"></param>
       /// <returns></returns>
        public static bool AliasValidate(string alias)
        {
            string where = " where product.BoardAlias='" + alias+"'";
            MKtPubcProduct[] productlist = (MKtPubcProduct[])MKtPubcProduct.SelectByWhere(where);
            if (productlist.Length > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 选择所有调查表类型
        /// </summary>
        /// <returns></returns>
        public static IList<MKtPubcProduct> SelectAll()
        {
            MKtPubcProduct product = new MKtPubcProduct();
            string where = " where product.IsValidate=1 order by product.ID ";
            return (IList<MKtPubcProduct>)MKtPubcProduct.SelectByWhere(where);
        }
        /// <summary>
        /// 选择周期数不为0的调查表
        /// </summary>
        /// <returns></returns>
        public static IList<MKtPubcProduct> SelectByCycNum()
        {
            MKtPubcProduct product = new MKtPubcProduct();
            string where = " where product.IsValidate=1 and product.CycNum<>0 order by product.ID ";
            return (IList<MKtPubcProduct>)MKtPubcProduct.SelectByWhere(where);
        }
    }
}
